package com.sleep.psy.external_server.business.resourse.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sleep.psy.external_server.business.resourse.entity.dto.Scale;
import com.sleep.psy.external_server.business.resourse.entity.vo.ScaleReq;
import com.sleep.psy.external_server.business.resourse.service.ResourseScaleService;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultEnum;
import com.sleep.psy.external_server.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 量表资源 前端控制器
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Api(tags = {"量表"})
@RestController
@RequestMapping("/resourseScale")
public class ResourseScaleController {

    protected Logger logger = LoggerFactory.getLogger(ResourseScaleController.class);

    @Autowired
    private ResourseScaleService resourseScaleService;

    /**
     * 获取量表（暂时弃用）
     * @author wenliguo
     * @date 2025/6/5 16:24
     * @param req 请求参数
     * @return com.sleep.psy.main_server.result.Result<com.baomidou.mybatisplus.core.metadata.IPage<com.sleep.psy.main_server.business2.scale.entity.dto.Scale>>
     */
    @PostMapping("/selectScale")
    @ApiOperation(value = "获取量表")
    public Result<IPage<Scale>> selectScale(@Valid @RequestBody ScaleReq req) {

        logger.info("获取量表参数:{}", JSON.toJSONString(req));
        try {
            return resourseScaleService.selectScale(req);
        } catch (Exception e) {
            logger.error("通过分类code获取量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过量表id获取量表
     * @author wenliguo
     * @date 2025/8/13 09:41
     * @param scaleId 量表id
     * @return com.sleep.psy.external_server.result.Result<com.sleep.psy.external_server.resourse.entity.dto.Scale>
    */
    @GetMapping("/selectScaleById")
    @ApiOperation(value = "通过量表id获取量表")
    public Result<Scale> selectScaleById(@RequestParam("scaleId") Integer scaleId) {

        try {
            return resourseScaleService.selectScaleById(scaleId);
        } catch (Exception e) {
            logger.error("通过量表id获取量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 获取自助训练量表
     * @author wenliguo
     * @date 2025/8/28 11:24
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @return com.sleep.psy.external_server.result.Result<com.baomidou.mybatisplus.core.metadata.IPage<com.sleep.psy.external_server.business.resourse.entity.dto.Scale>>
    */
    @GetMapping("/selectAutoTraingScales")
    @ApiOperation(value = "获取自助训练量表")
    public Result<IPage<Scale>> selectAutoTraingScales(@RequestParam(value = "pageNum", defaultValue = "1") Long pageNum, @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize) {

        try {
            return resourseScaleService.selectAutoTraingScales(pageNum, pageSize);
        } catch (Exception e) {
            logger.error("获取自助训练量表出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }
}

